//
//  ViewController.swift
//  mysigndemo
//
//  Created by dengwei on 16/6/2.
//  Copyright © 2016年 dengwei. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    private var scrollView: UIScrollView?
    
    let signBgView = UIView()
    let jifenBgView = UIView()
    let jifenLabel = UILabel()
    let jifenImg = UIImageView()
    let usernameLabel = UILabel()
    let moneyImg = UIImageView()
    let moneyLabel = UILabel()
    //
    let signButtonBgView = UIView()
    let signImageView = UIImageView()
    let signTextLabel = UILabel()
    
    //let xianView = UIView()
    //moreView
    let moreView = UIView()
    let morejifenView = UIView()
    let morejifenLabel = UILabel()
    let moreSmLabel = UILabel()
    let lineView = UIView()
    //微信晒图榜
    let wxImage = UIImageView()
    let weixinLabel = UILabel()
    let jiangli2Label = UILabel()
    let wx2Image = UIImageView()
    let line2View = UIView()

    //邀请好友
    let friendImage = UIImageView()
    let friendLabel = UILabel()
    let jiangli1Label = UILabel()
    let friend2Image = UIImageView()
    let line3View = UIView()
        //关注公众号
    let gzhImage = UIImageView()
    let gzhLabel = UILabel()
    let jiangli3Label = UILabel()
    let gzh2Image = UIImageView()
    let line4View = UIView()
    //
    let signBangView = UIView()
    let signbangButton = UIButton()
    let signgiftButton = UIButton()
    let signbang2Button = UIButton()
    let signgift2Button = UIButton()
    let line5View = UIView()
    
    private var signView = SignView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "我要签到"
        
        buildScrollView()
        buildSignBgView()
        buildMoreView()
        buildSignBang()
        buildSignGift()
    }
    
    private func buildScrollView() {
        scrollView = UIScrollView(frame: view.bounds)
        scrollView?.backgroundColor = LFBGlobalBackgroundColor
        scrollView?.contentSize.width = AppWidth
        scrollView?.alwaysBounceVertical = true
        view.addSubview(scrollView!)
    }
    
    func buildSignBgView(){
        //signBgView
        signBgView.frame = CGRectMake(0, 0,AppWidth, 310)
        signBgView.backgroundColor = LFBGlobalSignBackgroundColor
        self.scrollView!.addSubview(signBgView)
        
        jifenBgView.frame = CGRectMake(5, 5,AppWidth - 10, 64)
        jifenBgView.backgroundColor = LFBGlobalJifenBackgroundColor
        jifenBgView.layer.masksToBounds = true
        jifenBgView.layer.cornerRadius = 5
        //jifenBgView.alpha = 0.6
        self.signBgView.addSubview(jifenBgView)
        
        //jifenLabel
        jifenLabel.frame = CGRectMake(10, 5,60, 24)
        jifenLabel.textColor = UIColor.blackColor()
        jifenLabel.numberOfLines = 1
        jifenLabel.textColor = LFBGlobalSignTextColor
        jifenLabel.font = UIFont.systemFontOfSize(14)
        jifenLabel.text = "积分数量"
        self.jifenBgView.addSubview(jifenLabel)
        
        //jifenImg
        jifenImg.frame = CGRectMake(CGRectGetMaxX(jifenLabel.frame), 8,16, 16)
        jifenImg.image = UIImage(named:"sign_icon_jifen")
        self.jifenBgView.addSubview(jifenImg)
        
        //usernameLabel
        usernameLabel.frame = CGRectMake(AppWidth - 140, 15,120, 24)
        usernameLabel.numberOfLines = 1
        usernameLabel.textColor = LFBGlobalSignTextColor
        usernameLabel.font = UIFont.systemFontOfSize(14)
        usernameLabel.text = "谈笑一瞬间谈笑一瞬间"
        self.jifenBgView.addSubview(usernameLabel)
        
        //moneyImg
        moneyImg.frame = CGRectMake(10, CGRectGetMaxY(jifenLabel.frame) + 10,16, 16)
        moneyImg.image = UIImage(named:"sign_icon_glod")
        self.jifenBgView.addSubview(moneyImg)
        
        //moneyLabel
        moneyLabel.frame = CGRectMake(CGRectGetMaxX(moneyImg.frame) + 5, CGRectGetMaxY(jifenLabel.frame) + 5,80, 24)
        moneyLabel.textColor = UIColor.blackColor()
        moneyLabel.numberOfLines = 1
        moneyLabel.textColor = UIColor.redColor()
        moneyLabel.font = UIFont.systemFontOfSize(18)
        moneyLabel.text = "5000"
        self.jifenBgView.addSubview(moneyLabel)
        //signButtonBgView
        signButtonBgView.frame = CGRectMake(0, CGRectGetMaxY(jifenBgView.frame) ,AppWidth, 250)
        signButtonBgView.backgroundColor = UIColor(patternImage: UIImage(named:"sign_backimg")!)
        self.signBgView.addSubview(signButtonBgView)
        //signButton
        signImageView.frame = CGRectMake(AppWidth * 0.5 - 84, 5,169, 159)
        signImageView.image = UIImage(named:"sign_pig")
        signImageView.layer.shadowOpacity = 0.8
        signImageView.layer.shadowColor = UIColor.blackColor().CGColor
        signImageView.layer.shadowOffset = CGSize(width: 1, height: 1)
        self.signButtonBgView.addSubview(signImageView)
        //signTextLabel
        signTextLabel.frame = CGRectMake(AppWidth  * 0.5 - 100, 215 ,200, 30)
        signTextLabel.text = "已连续签到1天,累计积分5分"
        signTextLabel.numberOfLines = 1
        signTextLabel.textColor = LFBGlobalSignTextColor
        signTextLabel.font = UIFont.systemFontOfSize(14)
        self.signButtonBgView.addSubview(signTextLabel)
        /////设置允许交互属性
        signImageView.userInteractionEnabled = true
        
        /////添加tapGuestureRecognizer手势
        let tapSign = UITapGestureRecognizer(target: self, action: "signTapHandler:")
        signImageView.addGestureRecognizer(tapSign)
        
        
        //xianView
        //xianView.frame = CGRectMake(3, 230 ,AppWidth - 10, 16)
        //xianView.backgroundColor = UIColor(patternImage: UIImage(named:"sign_icon_xian")!)
        //self.signButtonBgView.addSubview(xianView)
        
    }
    
    private func buildMoreView(){
        moreView.frame = CGRectMake(0, CGRectGetMaxY(signBgView.frame) + 20,AppWidth, 320)
        moreView.backgroundColor = UIColor.whiteColor()
        self.scrollView!.addSubview(moreView)
        
        morejifenView.frame = CGRectMake(0, 0 ,AppWidth, 40)
        morejifenView.backgroundColor = UIColor.whiteColor()
        self.moreView.addSubview(morejifenView)
        //tview
        let tview = UIView(frame: CGRectMake(2, 0, 5, 40))
        tview.backgroundColor = LFBGlobalJifenTextColor
        self.moreView.addSubview(tview)
        
        //morejifenLabel
        morejifenLabel.frame = CGRectMake(10, 5,140, 30)
        morejifenLabel.textColor = LFBGlobalJifenTextColor
        morejifenLabel.numberOfLines = 1
        morejifenLabel.font = UIFont.systemFontOfSize(16)
        morejifenLabel.text = "赚取更多积分"
        self.moreView.addSubview(morejifenLabel)
        
        moreSmLabel.frame = CGRectMake(AppWidth - 85, 5,80, 24)
        moreSmLabel.numberOfLines = 1
        moreSmLabel.textColor = LFBGlobalJifenTextColor
        moreSmLabel.font = UIFont.systemFontOfSize(14)
        moreSmLabel.text = "积分说明>>"
        self.moreView.addSubview(moreSmLabel)
        moreSmLabel.userInteractionEnabled = true
        /////添加tapGuestureRecognizer手势
        let tapsmClick = UITapGestureRecognizer(target: self, action: "smClick:")
        moreSmLabel.addGestureRecognizer(tapsmClick)
        //lineView
        lineView.frame = CGRectMake(4, CGRectGetMaxY(morejifenView.frame) , AppWidth - 8, 1)
        lineView.backgroundColor = UIColor.lightGrayColor()
        lineView.alpha = 0.2
        self.moreView.addSubview(lineView)
        
        //wxImage
        wxImage.frame = CGRectMake(20, CGRectGetMaxY(lineView.frame) + 10,16, 16)
        wxImage.image = UIImage(named:"sign_icon_bweixin")
        self.moreView.addSubview(wxImage)
        //weixinLabel
        weixinLabel.frame = CGRectMake(CGRectGetMaxX(wxImage.frame) + 20, CGRectGetMaxY(lineView.frame) + 6,100, 24)
        weixinLabel.numberOfLines = 1
        weixinLabel.textColor = LFBGlobalFriendTextColor
        weixinLabel.font = UIFont.systemFontOfSize(14)
        weixinLabel.text = "微信晒周榜"
        self.moreView.addSubview(weixinLabel)
        weixinLabel.userInteractionEnabled = true
        /////添加tapGuestureRecognizer手势
        let shaituClick = UITapGestureRecognizer(target: self, action: "shaituClick:")
        weixinLabel.addGestureRecognizer(shaituClick)
        //jiangli2Label
        jiangli2Label.frame = CGRectMake(AppWidth - 120, CGRectGetMaxY(lineView.frame) + 6,90, 24)
        jiangli2Label.numberOfLines = 1
        jiangli2Label.textColor = LFBGlobalJiangliTextColor
        jiangli2Label.font = UIFont.systemFontOfSize(14)
        jiangli2Label.text = "奖励10积分"
        jiangli2Label.textAlignment = .Right
        self.moreView.addSubview(jiangli2Label)
        jiangli2Label.userInteractionEnabled = true
        let shaitu2Click = UITapGestureRecognizer(target: self, action: "shaituClick:")
        jiangli2Label.addGestureRecognizer(shaitu2Click)
        //
        wx2Image.frame = CGRectMake(AppWidth - 20, CGRectGetMaxY(lineView.frame) + 10,8, 15)
        wx2Image.image = UIImage(named:"sign_icon_bo")
        self.moreView.addSubview(wx2Image)
        //line2View
        line2View.frame = CGRectMake(6, CGRectGetMaxY(wxImage.frame) + 10 , AppWidth - 12, 1)
        line2View.backgroundColor = UIColor.lightGrayColor()
        line2View.alpha = 0.2
        self.moreView.addSubview(line2View)
        
        
        //friendImage
        friendImage.frame = CGRectMake(20, CGRectGetMaxY(line2View.frame) + 10,16, 16)
        friendImage.image = UIImage(named:"sign_icon_bfriend")
        self.moreView.addSubview(friendImage)
        //friendLabel
        friendLabel.frame = CGRectMake(CGRectGetMaxX(friendImage.frame) + 20, CGRectGetMaxY(line2View.frame) + 6,100, 24)
        friendLabel.numberOfLines = 1
        friendLabel.textColor = LFBGlobalFriendTextColor
        friendLabel.font = UIFont.systemFontOfSize(14)
        friendLabel.text = "邀请好友"
        self.moreView.addSubview(friendLabel)
        friendLabel.userInteractionEnabled = true
        /////添加tapGuestureRecognizer手势
        let yaoqingClick = UITapGestureRecognizer(target: self, action: "yaoqingClick:")
        friendLabel.addGestureRecognizer(yaoqingClick)
        //jiangli1Label
        jiangli1Label.frame = CGRectMake(AppWidth - 120, CGRectGetMaxY(line2View.frame) + 6,90, 24)
        jiangli1Label.numberOfLines = 1
        jiangli1Label.textColor = LFBGlobalJiangliTextColor
        jiangli1Label.font = UIFont.systemFontOfSize(14)
        jiangli1Label.textAlignment = .Right
        jiangli1Label.text = "奖励1000积分"
        self.moreView.addSubview(jiangli1Label)
        jiangli1Label.userInteractionEnabled = true
        /////添加tapGuestureRecognizer手势
        let yaoqing2Click = UITapGestureRecognizer(target: self, action: "yaoqingClick:")
        jiangli1Label.addGestureRecognizer(yaoqing2Click)
        //
        friend2Image.frame = CGRectMake(AppWidth - 20, CGRectGetMaxY(line2View.frame) + 10,8, 15)
        friend2Image.image = UIImage(named:"sign_icon_bo")
        self.moreView.addSubview(friend2Image)
        //line3View
        line3View.frame = CGRectMake(6, CGRectGetMaxY(friendImage.frame) + 10 , AppWidth - 12, 1)
        line3View.backgroundColor = UIColor.lightGrayColor()
        line3View.alpha = 0.2
        self.moreView.addSubview(line3View)
        
        //gzhImage
        gzhImage.frame = CGRectMake(20, CGRectGetMaxY(line3View.frame) + 10,16, 16)
        gzhImage.image = UIImage(named:"sign_icon_blook")
        self.moreView.addSubview(gzhImage)
        //gzhLabel
        gzhLabel.frame = CGRectMake(CGRectGetMaxX(gzhImage.frame) + 20, CGRectGetMaxY(line3View.frame) + 6,100, 24)
        gzhLabel.numberOfLines = 1
        gzhLabel.textColor = LFBGlobalFriendTextColor
        gzhLabel.font = UIFont.systemFontOfSize(14)
        gzhLabel.text = "关注公众号"
        self.moreView.addSubview(gzhLabel)
        gzhLabel.userInteractionEnabled = true
        /////添加tapGuestureRecognizer手势
        let guanzhuClick = UITapGestureRecognizer(target: self, action: "guanzhuClick:")
        gzhLabel.addGestureRecognizer(guanzhuClick)

        //jiangli3Label
        jiangli3Label.frame = CGRectMake(AppWidth - 130, CGRectGetMaxY(line3View.frame) + 6,100, 24)
        jiangli3Label.numberOfLines = 1
        jiangli3Label.textColor = LFBGlobalJiangliTextColor
        jiangli3Label.font = UIFont.systemFontOfSize(14)
        jiangli3Label.text = "抽取神秘大礼包"
        jiangli3Label.textAlignment = .Right
        self.moreView.addSubview(jiangli3Label)
        jiangli3Label.userInteractionEnabled = true
        let guanzhu2Click = UITapGestureRecognizer(target: self, action: "guanzhuClick:")
        jiangli3Label.addGestureRecognizer(guanzhu2Click)
        //gzh2Image
        gzh2Image.frame =  CGRectMake(AppWidth - 20, CGRectGetMaxY(line3View.frame) + 10,8, 15)
        gzh2Image.image = UIImage(named:"sign_icon_bo")
        self.moreView.addSubview(gzh2Image)
        //line4View
        line4View.frame = CGRectMake(6, CGRectGetMaxY(gzhImage.frame) + 10 , AppWidth - 12, 1)
        line4View.backgroundColor = UIColor.lightGrayColor()
        line4View.alpha = 0.2
        self.moreView.addSubview(line4View)
    }
    
    private func buildSignBang(){
        signBangView.frame = CGRectMake(0, CGRectGetMaxY(line4View.frame) + 20,AppWidth, 160)
        signBangView.backgroundColor = UIColor.whiteColor()
        self.moreView.addSubview(signBangView)
        //signbangImage
        signbangButton.frame = CGRectMake(40, 10,90,90)
        signbangButton.setTitleColor(LFBTextGreyColol, forState: .Normal)
        signbangButton.setBackgroundImage(UIImage(named:"sign_icon_bang_normal"), forState: .Normal)
        signbangButton.setBackgroundImage(UIImage(named:"sign_icon_bang_press"), forState: .Highlighted)
        self.signBangView.addSubview(signbangButton)
        //signbang2Button
        signbang2Button.frame = CGRectMake(40, CGRectGetMaxY(signbangButton.frame) + 10,100,30)
        signbang2Button.setTitleColor(LFBGlobalJiangliTextColor, forState: .Normal)
        signbang2Button.setTitle(" 签到周榜 ", forState: .Normal)
        signbang2Button.backgroundColor = UIColor.whiteColor()
        signbang2Button.layer.borderColor = LFBGlobalJiangliTextColor.CGColor
        signbang2Button.layer.borderWidth = 1
        signbang2Button.layer.cornerRadius = 5
        signbang2Button.layer.masksToBounds = true
        signbang2Button.layer.cornerRadius = 5
        signbang2Button.titleLabel?.font = UIFont.systemFontOfSize(14)
        self.signBangView.addSubview(signbang2Button)
        
        signbangButton.addTarget(self, action: "signbangAction:", forControlEvents: .TouchUpInside)
        signbang2Button.addTarget(self, action: "signbangAction:", forControlEvents: .TouchUpInside)
        
        //line5View
        line5View.frame = CGRectMake(AppWidth * 0.5 - 5, 20 , 1, 120)
        line5View.backgroundColor = UIColor.lightGrayColor()
        line5View.alpha = 0.2
        self.signBangView.addSubview(line5View)

    }
    
    private func buildSignGift(){
        //signbangImage
        signgiftButton.frame = CGRectMake(AppWidth - 40 - 90, 10,90,90)
        signgiftButton.setTitleColor(LFBTextGreyColol, forState: .Normal)
        signgiftButton.setBackgroundImage(UIImage(named:"sign_icon_gift_normal"), forState: .Normal)
        signgiftButton.setBackgroundImage(UIImage(named:"sign_icon_gift_press"), forState: .Highlighted)
        self.signBangView.addSubview(signgiftButton)
        //signgift2Button
        signgift2Button.frame = CGRectMake(AppWidth - 40 - 90, CGRectGetMaxY(signgiftButton.frame) + 10,100,30)
        signgift2Button.setTitleColor(LFBGlobalJiangliTextColor, forState: .Normal)
        signgift2Button.setTitle(" 积分商城 ", forState: .Normal)
        signgift2Button.backgroundColor = UIColor.whiteColor()
        signgift2Button.layer.borderColor = LFBGlobalJiangliTextColor.CGColor
        signgift2Button.layer.borderWidth = 1
        signgift2Button.layer.cornerRadius = 5
        signgift2Button.layer.masksToBounds = true
        signgift2Button.layer.cornerRadius = 5
        signgift2Button.titleLabel?.font = UIFont.systemFontOfSize(14)
        self.signBangView.addSubview(signgift2Button)
        
        signgiftButton.addTarget(self, action: "signgiftAction:", forControlEvents: .TouchUpInside)
        signgift2Button.addTarget(self, action: "signgiftAction:", forControlEvents: .TouchUpInside)
    }
    //////手势处理函数
    func signTapHandler(sender:UITapGestureRecognizer) {
        
        print("signTapHandler")
       
        signImageView.image = UIImage(named:"sign_pig_success")
        self.view.addSubview(signView)
        signView.signVC = self
        signView.showSignView(CGRectMake(10, 140, AppWidth - 20, 285))
        signView.signLabel.text = "签到成功 积分 + 10"
        signView.signPaimingLabel.text = "周榜排名1314名"
        signView.signViewClick = { (type) -> () in
            //print(type)
            switch type {
            case .WEIXIN:
                //print("WEIXIN")
                break
            case .SHARE:
                //print("SHARE")
                break
            }
        }
    }
    
    func smClick(sender:UITapGestureRecognizer) {
        
        print("smClick")
    }
    
    func yaoqingClick(sender:UITapGestureRecognizer) {
        
        print("yaoqingClick")
    }

    func shaituClick(sender:UITapGestureRecognizer) {
        
        print("shaituClick")
    }
    
    func guanzhuClick(sender:UITapGestureRecognizer) {
        
        print("guanzhuClick")
    }

    func signbangAction(sender:UIButton){
        print("signbangAction")
    }
    
    func signgiftAction(sender:UIButton){
        print("signgiftAction")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func alertView(){
        let alertController = UIAlertController(title: "升级提示", message: "马上更新", preferredStyle: UIAlertControllerStyle.Alert)
        //let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
        //let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default, handler: nil)
        let updateAction: UIAlertAction = UIAlertAction(title: "马上更新", style: .Default) { (action: UIAlertAction!) -> Void in
           print("start")
        }
        alertController.addAction(updateAction)
        //alertController.addAction(cancelAction)
       // alertController.addAction(okAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
    
    override func viewDidAppear(animated: Bool) {
        scrollView?.contentSize.height = CGRectGetMaxY(self.moreView.frame) + 20
    }

}

